package com.talent.web.mqtt;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;


/**
 * @Author: hszhou
 * @Date: 2025/1/8 20:58
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Configuration
public class MqttConfiguration {

    @Autowired
    private MqttConfigurationProperties mqttConfigurationProperties;
    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory mqttPahoClientFactory = new DefaultMqttPahoClientFactory();
        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setCleanSession(true);
        mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
        mqttConnectOptions.setUserName(mqttConfigurationProperties.getUsername());
        mqttConnectOptions.setPassword(mqttConfigurationProperties.getPassword().toCharArray());

        mqttConnectOptions.setServerURIs(new String[]{mqttConfigurationProperties.getUrl()});
        mqttPahoClientFactory.setConnectionOptions(mqttConnectOptions);
        return mqttPahoClientFactory;

    }
}
